package cn.net.autocode.platform.design;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.platform.config.SpringProfileConfig;
import cn.net.autocode.platform.design.handle.AutoCodeHandle;
import cn.net.autocode.platform.design.vo.AppInfo;
import cn.net.autocode.platform.design.vo.DeveloperInfo;
import cn.net.autocode.platform.design.webclient.AcWebClient;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;

@RestController()
@RequestMapping("/design/developer")
public class Developer {

    private final SpringProfileConfig profileConfig;

    private final AcWebClient acWebClient;

    public Developer(SpringProfileConfig profileConfig, AcWebClient acWebClient) {
        this.profileConfig = profileConfig;
        this.acWebClient = acWebClient;
    }

    /**
     * 页面登录
     */
    @PostMapping(value = "/checkToken", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultT checkToken() {

        if(!"dev".equals(profileConfig.getProfile())){
            return ResultT.success(new JSONObject());
        }

        //授权信息
        AppInfo appInfo = AutoCodeHandle.getAppInfo();


        if(appInfo==null){
            JSONObject failJson = new JSONObject();
            failJson.put("code", "没有授权信息，请先获取本项目的license.lic授权证书");
            return ResultT.success(failJson);
        }

        //用户信息
        JSONObject developer = new JSONObject();
        DeveloperInfo developerInfo = AutoCodeHandle.getDeveloperInfo();
        if(developerInfo ==null){
            developer.put("email", "请先进行登录");
            developer.put("phone", "请先进行登录");
            developer.put("fullname", "请先进行登录");
            developer.put("username", "");
            developer.put("token", "请先进行登录");
        }else{
            developer.put("email", developerInfo.getEmail());
            developer.put("phone", developerInfo.getPhone());
            developer.put("fullname", developerInfo.getFullname());
            developer.put("username", developerInfo.getUsername());
            developer.put("token", developerInfo.getToken());
        }
        developer.put("appid",appInfo.getAppid());
        developer.put("code",appInfo.getCode()+"【"+appInfo.getAppid()+"】");
        return ResultT.success(developer);
    }


    @PostMapping(value = "/openDesign", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultT openDesign(@RequestBody JSONObject param) {
        //授权信息
        AppInfo appInfo = AutoCodeHandle.getAppInfo();

        if(appInfo == null){
            return ResultT.fail("没有授权信息，请先获取本项目的license.lic授权证书");
        }

        //用户信息
        DeveloperInfo developerInfo = AutoCodeHandle.getDeveloperInfo();
        if(developerInfo==null){
            return ResultT.fail("请先登录开发者信息");
        }else{
            //param.put("licenseToken",developerInfo.getToken());
            String random = acWebClient.openDesign(param);
            JSONObject rs = new JSONObject();
            rs.put("ipCode",random);
            return ResultT.success(rs);
        }
    }

}
